package cmd

import (
	"dockertools/dockerdemo"
	"fmt"
	"github.com/spf13/cobra"
	"os"
)

var (
	imagename string
	tag string
	num string
)


// searchtagCmd represents the searchtag command
var searchtagCmd = &cobra.Command{
	Use:   "searchtag",
	Short: "查询镜像可用标签",
	Long: `usage:
		docker-search-tag NAME [TAG] [NUM]
		list mysql tags:
		
		docker-search-tag mysql
		list mysql tags by filter 8. (search version 8.x)
		
		docker-search-tag mysql 8.
		or list 10 tags(default is 100):
		
		docker-search-tag mysql 8. 10
		docker-search-tag mysql _  10
		here, can use _ igor second flag.
		
		results:
		
		Tags for mysql:
		
		TAG            SIZE(MB)   LASTPUSH
		latest              144   2021-12-02T11:43:26Z
		8.0.4-rc             83   2018-03-14T08:42:43Z
		8.0.4                83   2018-03-14T08:42:43Z
		8.0.3               107   2018-02-17T09:43:00Z
		8.0.1                86   2017-06-24T13:36:11Z
		8.0.0               126   2017-04-10T23:29:13Z
		8.0                 144   2021-12-02T11:43:21Z
		8                   144   2021-12-02T11:43:20Z
		5.7.36              147   2021-12-02T11:43:09Z
		5.7.35              147   2021-10-12T16:42:35Z
		5.6.28              106   2016-02-02T22:41:46Z
		5.6.27              106   2015-12-17T03:17:56Z
		5.6                  98   2021-12-02T11:43:04Z
		5.5                  63   2019-05-10T23:43:32Z
		5                   147   2021-12-02T11:42:49Z`,
	Run: func(cmd *cobra.Command, args []string) {
		if imagename == "" {
			fmt.Println("请通过 -i 或者 --imagename 指定要查询的镜像名, 或者 dockertools.exe searchtag -h")
			os.Exit(1)
		}
		dockerdemo.SearchTag(imagename, tag, num)
	},
}

func init() {
	rootCmd.AddCommand(searchtagCmd)

	searchtagCmd.Flags().StringVarP(&imagename, "imagename", "i", "",
		"用于指定要检索镜像便签的镜像名，-i mysql 搜索 mysql 相关镜像")
	searchtagCmd.Flags().StringVarP(&tag, "tag", "t", "",
		"用于需要匹配标签的关键字, -t 5 包含 5 关键字的标签")
	searchtagCmd.Flags().StringVarP(&num, "num", "n", "10",
		"用于指定需要显示便签的数量级 -n 10 显示 10 个标签")
}
